import { Toast } from "vant";
// vite中简写动态图片路径
export const getAssetsFile = (url: string,fileName:string='images') => {
    return new URL(`../assets/${fileName}/${url}`, import.meta.url).href
}
// 用户名称6字省略
export const tipsName = (val:any ='',num:number = 6,sign:number=3) => {
    let arr = val.split('...')
    if(arr[arr.length - 1] == '') return val
    if(val.length > num) {
        let signtext = ''
        for(let i=1;i<=sign;i++) {
            signtext+='.'
        }
        val = val.slice(0,num) + signtext
    }
    return val
};

// vant提示框
export const xmtoast = (message:any = '') => {
    Toast({
      message,
      className:'xmToast',
    })
}
// vant加载框
export const xmloading = (message = '加载中...') => {
    Toast.loading({
        message,
        forbidClick: true,
        loadingType:'spinner',
        duration:0,
        className:'xmloading',
    })
}
const http = import.meta.env.VITE_DOWNLOAD;
console.log(http);
const getPosType = () => {
    var agent = navigator.userAgent.toLowerCase();
    var isMac = /macintosh|mac os x/i.test(navigator.userAgent);
    if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
        return 'window'
    }
    if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
        return 'window'
    }
    if (isMac) {
        return 'mac'
    }
}
export const downloadApp = () => {
    if (/(Android)/i.test(navigator.userAgent)) {
        window.location.href = http + '/system/version/get?channel_id=200001';
        return 'Android'
    } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        window.location.href = http + '/system/version/get?channel_id=300001';
        return 'ios'
    }else if (getPosType() == 'window') {
        window.location.href = http + '/system/version/get?channel_id=200001';
        return 'window'
    }else if (getPosType() == 'mac') {
        window.location.href = http + '/system/version/get?channel_id=300001';
        return 'mac'
    }
}

export const copyText = (e:string | number) => {
    let dataNum = e + ''
    var element = createElement(dataNum);
    element.select();
    element.setSelectionRange(0, element.value.length);
    document.execCommand("copy");
    element.remove();
    xmtoast("已复制到剪切板");
};
const createElement = (text = "") => {
    var isRTL = document.documentElement.getAttribute("dir") === "rtl";
    var element = document.createElement("textarea");
    // 防止在ios中产生缩放效果
    element.style.fontSize = "12pt";
    // 重置盒模型
    element.style.border = "0";
    element.style.padding = "0";
    element.style.margin = "0";
    // 将元素移到屏幕外
    element.style.position = "absolute";
    element.style[isRTL ? "right" : "left"] = "-9999px";
    // 移动元素到页面底部
    let yPosition = window.pageYOffset || document.documentElement.scrollTop;
    element.style.top = `${yPosition}px`;
    //设置元素只读
    element.setAttribute("readonly", "");
    element.value = text;
    document.body.appendChild(element);
    return element;
};